package com.haina.vote.mapper;

import com.haina.vote.entity.IpInfo;
import com.haina.vote.entity.Vote;
import com.haina.vote.entity.VoteItem;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface VoteMapper {
    //查询所有投票
    @Select("select * from tb_vote")
    public List<Vote> findAllVote();

    //新增投票
    @Insert("insert into tb_vote(title,type,remark,start_time,end_time,create_time)values (#{title},#{type},#{remark},#{start_time},#{end_time},#{create_time})")
    public void addVote(Vote vote);
    //删除投票
    @Delete("delete from tb_vote where id=#{id}")
    public void deleteVote(Integer id);

    //通过id查询投票
    @Select("select * from tb_vote where id=#{id}")
    public Vote findVoteById(Integer id);

    //通过id查询投票类型
    @Select("select type from tb_vote where id=#{vote_id}")
    public Integer findVoteTypeById(Integer id);

    //查询全部选项
    @Select("select * from tb_vote_items")
    @Results({
            @Result(id = true,property = "item_id",column = "item_id"),
            @Result(property = "item_title",column = "item_title"),
            @Result(property = "item_remark",column = "item_remark"),
            @Result(property = "file_url",column = "file_url"),
            @Result(property = "times",column = "times"),
            @Result(property = "vote",column = "vote_id",javaType = Vote.class,one =@One(select = "com.haina.vote.mapper.VoteMapper.findVoteById")),
    })
    public List<VoteItem> findAllVoteItem();

    //新增选项
    @Insert("insert into tb_vote_items(item_title,file_url,item_remark,vote_id)values(#{item_title},#{file_url},#{item_remark},#{vote_id})")
    public void addItem(VoteItem voteItem);

    //查询投票项的信息
    @Select("select * from tb_vote_items where vote_id=#{id}")
    @Results({
            @Result(id = true,property = "item_id",column = "item_id"),
            @Result(property = "item_title",column = "item_title"),
            @Result(property = "item_remark",column = "item_remark"),
            @Result(property = "file_url",column = "file_url"),
            @Result(property = "times",column = "times"),
            @Result(property = "vote_id",column = "vote_id"),
            @Result(property = "vote",column = "vote_id",javaType = Vote.class,one =@One(select = "com.haina.vote.mapper.VoteMapper.findVoteById")),
            @Result(property = "votetype",column = "vote_id",one =@One(select = "com.haina.vote.mapper.VoteMapper.findVoteTypeById")),
    })
    public List<VoteItem> findVoteInfo(Integer id);
    //查询投票信息用做关联选项
    @Select("select id,title from tb_vote")
    public List<Vote> findVote();

    //根据ip地址查询IP的信息
    @Select("select * from tb_ip_info where ipaddress=#{ip} and vote_id=#{vote_id}")
    public IpInfo findIpInfo(@Param("ip") String ip,@Param("vote_id")Integer vote_id);

    //添加ip记录
    @Insert("insert into tb_ip_info(ipaddress,number,request_time,vote_id) values(#{ipaddress},#{number},#{request_time},#{vote_id})")
    public void addIpInfo(IpInfo ipInfo1);
    //更新ip访问次数
    @Update("update tb_ip_info set number=#{req_num} where id=#{ip_id}")
    public void updateIpInfo(@Param("req_num") Integer req_num,@Param("ip_id") Integer ip_id);
    //查询当前选项的票数
    @Select("select times from tb_vote_items where item_id=#{item_id}")
    public Integer findItemTimes(Integer item_id);
    //更新当前选项的总票数
    @Update("update tb_vote_items set times=#{times} where item_id=#{item_id}")
    public void updateItemTimes(@Param("item_id")Integer item_id, @Param("times")Integer times);
    //删除选项
    @Delete("delete from tb_vote_items where item_id=#{item_id}")
    public void deleteItem(@Param("item_id") Integer item_id);
    //查询选项详情
    @Select("select * from tb_vote_items where item_id=#{item_id}")
    @Results({
            @Result(id = true,property = "item_id",column = "item_id"),
            @Result(property = "item_title",column = "item_title"),
            @Result(property = "item_remark",column = "item_remark"),
            @Result(property = "file_url",column = "file_url"),
            @Result(property = "times",column = "times"),
            @Result(property = "vote_id",column = "vote_id"),
            @Result(property = "vote",column = "vote_id",javaType = Vote.class,one =@One(select = "com.haina.vote.mapper.VoteMapper.findVoteById"))
    })
    public VoteItem findItemById(@Param("item_id") Integer item_id);
    //通过投票id查询该投票下面的所有选项
    @Select("select * from tb_vote_items where vote_id=#{id}")
    public List<VoteItem> findItemByVoteId(@Param("id") Integer id);
    @Delete("delete from tb_ip_info where vote_id=#{vote_id}")
    public void deleteAllIpByVoteId(Integer vote_id);
    @Update("update tb_vote_items set item_title=#{item_title},item_remark=#{item_remark},file_url=#{file_url},vote_id=#{vote_id} where item_id=#{item_id}")
    public void updateItem(VoteItem voteItem);
    @Update("update tb_vote set title=#{title},type=#{type},remark=#{remark},start_time=#{start_time},end_time=#{end_time} where id=#{id}")
    public void updateVote(Vote vote);
}
